import json
import openpyxl
import requests

wb = openpyxl.Workbook()
ws = wb.active
ws.append(['顺序','热搜分类','热搜关键词'])
result_str = "微博热搜"
url = requests.get("https://weibo.com/ajax/side/hotSearch")
# url.encoding= "gbk"
# print(url.text)
data = json.loads(url.text)['data']['realtime']
for i in data[:15]:
    # print(i)
    try:
        # print(f'热搜：{i["realpos"]}, 热搜分类[{i["category"]}], 热搜关键词：{i["word"]}')
        # print(f'{i["realpos"]} [{i["category"]}] {i["word"]}')
        ws.append([i["realpos"],i["category"],i["word"]])
        result_str += f'\n{i["realpos"]} [{i["category"]}] {i["word"]}'
    except:
        pass

print(result_str)


#发送vx消息
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d230f478-24eb-4780-a45d-0e8fe951bded'
headers = {'Content-Type': 'application/json'}
#文本
#带超链接
#article ="<a href=\"http://baidu.com\">邮件中心视频实况</a>"


data = {
    "msgtype": "text",
    "text": {
        "content": result_str
    },
    "safe": 0,
    "enable_id_trans": 0,
    "enable_duplicate_check": 0,
    "duplicate_check_interval": 1800
}
response = requests.post(url, headers=headers, data=json.dumps(data))

# wb.save("热搜.xlsx")
